<?php
/**
 * 后台用户相关model
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;
use yii\data\Pagination;
use yii\data\ArrayDataProvider;

class Admin extends ActiveRecord
{
    /**
     * 获取admin列表
     */
    public function getAdminList( $params = array() )
    {
        $data = array(
            'list' => array(),
            'count' => 0,
        );
        $admin = self::find();
        $admin->select('*');
        
        if(isset($params['ids']) && is_array($params['ids'])){
            $ids = implode(',',$params['ids']);
            $admin->andWhere("id in ({$ids})");
        }else{
            $page = $params['page'];
            $offset = 0;
            $limit = $params['pageSize'];
            if($page > 0){
                $offset = ($page - 1) * $limit;
            }
            $count = $admin->count();
            $admin->offset($offset)->limit($limit);
        }
        $result = $admin->asArray()->all();
        $data['list'] = $result ? $result : array();
        $data['count'] = isset($count) ? $count : 0;
        return $data;
    }
    
    /**
     * 获取
     */
    public function getAdmins( $params = array() )
    {
        $admin = self::find();
        $admin->select('*');
        if(isset($params['type'])){
            $admin->andWhere(array('type'=>$params['type']));
        }
        if(isset($params['pid'])){
            $admin->andWhere(array('pid'=>$params['pid']));
        }
        $result = $admin->asArray()->all();
        return $result;
    }
    
    /**
     * 根据id获取信息
     */
    public static function getById($id)
    {
        $admin = self::find();
        $admin->select('*');
        $admin->where('id = '.$id);
        $info = $admin->asArray()->one();
        return $info;
    }
    
    /**
     * 充值
     */
    public static function getChargeSum($ch)
    {
        $sql_user = "select user_id from xs_user where channel_id= :ch";
        $user = Yii::$app->db->createCommand($sql_user)->bindValue(':ch',$ch)->queryAll();
        if($user){
            $ids = implode(',',array_column($user,'user_id'));
            $sql = "select SUM(pay_money) as money,count(id) as cnt from xs_recharge where user_id in ({$ids}) and status=1";
            $data = Yii::$app->db->createCommand($sql)->queryOne();
        }else{
            $data = array(
                'money' => 0,
                'cnt' => 0,
            );
        }
        return $data;
    }
    
    /**
     * 充值
     */
    public static function getConsumeSum($ch)
    {
        $sql_user = "select user_id from xs_user where channel_id= :ch";
        $user = Yii::$app->db->createCommand($sql_user)->bindValue(':ch',$ch)->queryAll();
        if($user){
            $ids = implode(',',array_column($user,'user_id'));
            $sql = "select SUM(pay_coin) as money,count(id) as cnt from xs_user_consume where user_id in ({$ids}) and status=1";
            $data = Yii::$app->db->createCommand($sql)->queryOne();
        }else{
            $data = array(
                'money' => 0,
                'cnt' => 0,
            );
        }
        return $data;
    }
    
    /**
     * 推文数
     */
    public static function getTweetCount($ch)
    {
        $sql_count = "select count(*) as cnt from xs_tweet where channel_id= :ch";
        $data = Yii::$app->db->createCommand($sql_count)->bindValue(':ch',$ch)->queryOne();
        return $data;
    }
}
